package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ItemGroup {
	 Connect con = new Connect(); 
	 Connection conn = con.Connect();
	 ResultSet rs = null;
	 SQLiteDBClass myDb;
	 SQLiteDatabase db;
	 Context context;
	 private static final String TABLE_ItemGroup = "_ItemGroup";
	 public Context getContext() {
		return context;
	 }

	 public void setContext(Context context) {
		this.context = context;
	 }
	 
	String StrParamItemGroupMainCode=null;
	
	private String OID,
		    UsrOpt,
		    IsActive,
		    IsDeleted,
			ItemGroupCode,
			ItemGroupDesc,
			ItemGroupMainCode;
	
    public ItemGroup() {ItemGroupCode=ItemGroupDesc=ItemGroupMainCode="";
    }
	
    public ItemGroup(
    		String OID,
    		String UsrOpt,
    		String IsActive,
    		String IsDeleted,
    		String ItemGroupCode,
    		String ItemGroupDesc,
    		String ItemGroupMainCode
    )
    
    {
    	this.OID = OID;
    	this.UsrOpt = UsrOpt;
    	this.IsActive = IsActive;
    	this.IsDeleted = IsDeleted;
    	this.ItemGroupCode = ItemGroupCode;
    	this.ItemGroupDesc = ItemGroupDesc;
    	this.ItemGroupMainCode = ItemGroupMainCode;
    }
    
    //--------------Parameter----------//
  	public String getStrParamItemGroupMainCode() {
  		return StrParamItemGroupMainCode;
  	}

  	public void setStrParamItemGroupMainCode(String strParamItemGroupMainCode) {
  		StrParamItemGroupMainCode = strParamItemGroupMainCode;
  	}
    
    public String getOID() {
		return OID;
	}

	public void setOID(String oID) {
		OID = oID;
	}

	public String getUsrOpt() {
		return UsrOpt;
	}

	public void setUsrOpt(String usrOpt) {
		UsrOpt = usrOpt;
	}

	public String getIsActive() {
		return IsActive;
	}

	public void setIsActive(String isActive) {
		IsActive = isActive;
	}

	public String getIsDeleted() {
		return IsDeleted;
	}

	public void setIsDeleted(String isDeleted) {
		IsDeleted = isDeleted;
	}
	
    public void setItemGroupCode(String ItemGroupCode)
	{
		this.ItemGroupCode = ItemGroupCode;
	}
	public void setItemGroupDesc(String ItemGroupDesc)
	{
		this.ItemGroupDesc = ItemGroupDesc;
	}
	public void setItemGroupMainCode(String ItemGroupMainCode)
	{
		this.ItemGroupMainCode = ItemGroupMainCode;
	}

	public String getItemGroupCode()
	{
		return this.ItemGroupCode;
	}
	public String getItemGroupDesc()
	{
		return this.ItemGroupDesc;
	}
	public String getItemGroupMainCode()
	{
		return this.ItemGroupMainCode;
	}
	
	public ResultSet selectItemGroup(String StrItemGroupDesc,String ItemGroupMainCode) throws Exception {
		String  StrSql     = "SELECT [OID]," +
				"[LastModified]," +
				"[ModifyDate]," +
				"[ModifyTime]," +
				"[ModifiedBy]," +
				"[UserModify]," +
				"[CreatedDate]," +
				"[CreateDate]," +
				"[CreateTime]," +
				"[CreatedBy]," +
				"[UserCreate]," +
				"[IsActive]," +
				"[IsDeleted]," +
				
				"[ItemGroupCode]," +
				"[ItemGroupDesc]," +
				"[ItemGroupMainCode]," +
				"[ItemGroupMainDesc] " +
				
				"FROM [dbo].[udfItemGroup]() " +
				"WHERE [IsDeleted]='0' "; 

		if (!StrItemGroupDesc.equals("")) {
            StrSql += "AND [ItemGroupDesc] LIKE '%" + StrItemGroupDesc + "%'";
        }
		if (!ItemGroupMainCode.equals("")) {
            StrSql += "AND [ItemGroupMainCode] =  "+ ItemGroupMainCode + " ";
        }
    	
    	return con.selectData(StrSql);
	 }
	
	public ResultSet defineItemGroup() throws Exception {
        String StrSql = "SELECT [OID]," +
        		"[ItemGroupCode]," +
        		"[ItemGroupDesc] " +
        		"FROM [dbo].[udfItemGroup] () " +
        		"WHERE [IsDeleted]='0' ";

        StrSql += "ORDER BY [ItemGroupDesc] ASC";

        return con.selectData(StrSql);
    }
	
	public ResultSet defineItemGroupFromItemGruopMain() throws Exception {
		String StrSql = "SELECT [OID]," +
				"[ItemGroupCode]"+
				",[ItemGroupDesc]"+
				",[ItemGroupMainCode]"+
				",[ItemGroupMainDesc]"+	
	
				"FROM [dbo].[udfItemGroup] ()" +
				"WHERE [IsDeleted] = '0' ";
	
		if (getStrParamItemGroupMainCode()!=null) {
		StrSql += "AND [ItemGroupMainCode] = '" + getStrParamItemGroupMainCode() + "' ";
		}
	
		StrSql += "ORDER BY [ItemGroupDesc] ASC";
		
		return con.selectData(StrSql);
	}
	
	 public void closeConnecttion() throws Exception {
    	 if (conn != null) {
             conn.close();
         }
    }
	 
	 public void DEFINE_ItemGroup() {
			rs = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			try {
				rs = selectItemGroup("", "");
				while (rs.next()) {
					ContentValues Val = new ContentValues();
					Val.put("ItemGroupOID", rs.getString("OID"));

					Val.put("ItemGroupCode", rs.getString("ItemGroupCode"));
					Val.put("ItemGroupDesc", rs.getString("ItemGroupDesc"));
					Val.put("ItemGroupMainCode", rs.getString("ItemGroupMainCode"));
					Val.put("ItemGroupMainDesc", rs.getString("ItemGroupMainDesc"));

					db.insert(TABLE_ItemGroup, null, Val);
				}
			} catch (Exception e) {
				Log.d("TABLE_ItemGroup", "Create Table not Successfully.");
			}finally{
				try {
					closeConnecttion();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			Log.d("TABLE_ItemGroup", "Create Table Successfully.");
			db.close();
		}
	 
	 public String[][] SelectAll_ItemGroup(String ItemGroupMainOID) {
			try {
				String arrData[][] = null;
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				String strSQL = "SELECT  ItemGroupOID As _id , * FROM "
						+ TABLE_ItemGroup;
				if (!ItemGroupMainOID.equals("")) {
					strSQL += " WHERE [ItemGroupMainCode] LIKE '"
							+ ItemGroupMainOID + "' ORDER BY [ItemGroupOID] ASC";
				}
				Cursor cursor = db.rawQuery(strSQL, null);
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							arrData[i][3] = cursor.getString(4);
							arrData[i][4] = cursor.getString(5);

							i++;
						} while (cursor.moveToNext());

					}
				}
				cursor.close();

				return arrData;

			} catch (Exception e) {
				return null;
			}
		}

	
}
